<script setup lang="ts">
import {reactive, ref} from "vue";
import {addBodyAPI, deleteBodyAPI} from "@/api/resource.ts";
import {Delete, Edit, InfoFilled} from "@element-plus/icons-vue";
import Preview from "@/components/Preview.vue";
import {ElMessage, ElNotification} from "element-plus";
import {useBodyStore} from "@/store/body.ts";

const {getBodyList, bodyList} = useBodyStore()
const dialogVisible = ref(false)
const viewerVisible = ref(false);
const currentImage = ref('');
const ruleForm = reactive({
  name: '',
  nickname: '',
  imgUrl: ''
})

// 图片预览
const previewImage = (image) => {
  currentImage.value = image;
  viewerVisible.value = true;
}

// 关闭图片预览
const closeImageViewer = () => {
  viewerVisible.value = false;
};

// 上传部位图片成功回调
const onSuccessUpload = async (response) => {
  ruleForm.imgUrl = response.data
}

// 添加部位
const addBody = async () => {
  const res = await addBodyAPI(ruleForm)
  dialogVisible.value = false
  if (res.code === 0) {
    ElMessage({
      type: "success",
      message: res.message
    })
  }
  await getBodyList()
}

// 编辑部位
const EditBody = () => {
  ElNotification({
    title: 'Warning',
    message: '为减缓服务器压力，暂关闭编辑接口及数据回显',
    type: 'warning',
  })
}

// 删除部位
const deleteBody = async (id) => {
  const res = await deleteBodyAPI(id)
  if (res.code === 0) {
    ElMessage({
      type: 'success',
      message: res.message
    })
    await getBodyList()
  }
}
</script>

<template>
  <el-card class="box-card">
    <div class="card-body-title">
      <el-button type="primary" @click="dialogVisible = true">添加部位</el-button>
    </div>
    <div class="card-body-table">
      <el-table
          border
          class="el-table-box"
          ref="multipleTableRef"
          :data="bodyList"
          style="width: 100%"
      >
        <el-table-column label="#" width="60" align="center" type="index"/>
        <el-table-column prop="_id" align="center" label="id" width="260"/>
        <el-table-column prop="nickname" align="center" label="昵称"/>
        <el-table-column prop="name" align="center" label="名称"/>
        <el-table-column prop="name" align="center" label="图片" width="100">
          <template #default="{row}">
            <el-button @click="previewImage(row.imgUrl)">预览</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="控制" width="220">
          <template #default="{row}">
            <div class="operator-btn">
              <el-button type="primary" size="default" @click="EditBody" :icon="Edit as any" circle/>
              <el-popconfirm
                  width="200"
                  @confirm="deleteBody(row._id)"
                  confirm-button-text="确定"
                  cancel-button-text="取消"
                  :icon="InfoFilled as any"
                  icon-color="#f36b6c"
                  title="您确定删除此部位吗?"
              >
                <template #reference>
                  <el-button type="danger" size="default" :icon="Delete as any" circle/>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <Preview :visibleProp="viewerVisible" :imageLinkProp="currentImage" :onClose="closeImageViewer"/>
  </el-card>
  <el-dialog v-model="dialogVisible" title="添加部位">
    <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        style="padding: 0 50px"
        class="demo-ruleForm"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name" type="text" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="昵称" prop="checkPass">
        <el-input v-model="ruleForm.nickname" type="text" autocomplete="off"/>
      </el-form-item>
    </el-form>
    <el-upload
        class="upload-demo"
        drag
        :on-success="onSuccessUpload"
        action="http://116.62.147.119:3636/api/uploadBody"
    >
      <el-icon class="el-icon--upload">
        <upload-filled/>
      </el-icon>
      <div class="el-upload__text">
        点击这里 <em>上传</em>
      </div>
    </el-upload>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addBody">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">
.box-card {
  padding: 0 50px;

  .card-body-title {
    height: 35px;
    display: flex;
    justify-content: flex-end;
  }

  .card-body-table {
    margin-top: 20px;

    .el-table-box {
      height: calc(90vh - 140px);
    }
  }
}
</style>